package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.entity.InStore;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.mapper.InStoreMapper;
import com.example.warehouse_manager.mapper.ProductMapper;
import com.example.warehouse_manager.mapper.PurchaseMapper;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.InStoreService;
import com.example.warehouse_manager.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Yang
* @description 针对表【in_store(入库单)】的数据库操作Service实现
* @createDate 2024-04-09 15:04:27
*/
@Service
public class InStoreServiceImpl implements InStoreService{

    @Autowired
    private InStoreMapper inStoreMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private ProductMapper productMapper;

    //添加入库单
    @Override
    @Transactional
    public Result saveInStore(InStore inStore,Integer byId) {
        int i = inStoreMapper.insertStore(inStore);
        if (i>0){
            //修改订单状态为已入库
            int j = purchaseMapper.setIsInById(byId);
            if (j>0){
                return Result.ok("入库单添加成功");
            }
            return Result.err(Result.CODE_ERR_BUSINESS,"入库单添加失败");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"入库单添加失败");
    }

    //分页查询入库单
    @Override
    public Page queryInStorePage(Page page, InStore inStore) {

        //查询入库单行数
        Integer inStoreCount = inStoreMapper.findInStoreCount(inStore);
        //分页查询入库单
        List<InStore> inStorePage = inStoreMapper.findInStorePage(page, inStore);
        //封装分页信息
        page.setTotalNum(inStoreCount);
        page.setResultList(inStorePage);

        return page;
    }

    //确认入库
    @Override
    @Transactional
    public Result inStoreConfirm(InStore inStore) {
        //修改入库单状态
        int i = inStoreMapper.setIsInById(inStore.getInsId());
        if (i>0){
            //修改商品库存
            int j = productMapper.setInventById(inStore.getProductId(), inStore.getInNum());
            if (j>0){
                return Result.ok("入库单确认成功");
            }
            return Result.err(Result.CODE_ERR_BUSINESS,"入库单确认失败！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"入库单确认失败！");
    }
}




